<?php
class Month 
{

  public $day_count;
  public $year;
  public $days;
  public $month_name;
  public $month_number;

  public function __construct($month, $year=-1) {
    $months = Array(
    1=>'Janvier',
    2=>'Février',
    3=>'Mars',
    4=>'Avril',
    5=>'Mai',
    6=>'Juin',
    7=>'Juillet',
    8=>'Août',
    9=>'Septembre',
    10=>'Octobre',
    11=>'Novembre',
    12=>'Décembre'
    );
    $year = ($year == -1) ? date('Y') : $year;
    $this->month_name=$months[$month];
    $this->month_number = $month;
    if (strlen($this->month_number) == 1) {
      $this->month_number = "0".$this->month_number;
    }
    $this->year = $year;
    $time = mktime(0, 0, 0, $month, 1, $year);
    $this->day_count = date('t', $time);
    for($i=1; $i<=$this->day_count; $i++) {
      if (strlen($i) == 1) {
        $i = "0".$i;
      }
      $this->days[] = $i;
    }
  }

  public function getMonth() {

  }

  public function info() {
    $date = new DateTime();
    echo "Mois -> ".$this->month_name;
    echo '<br>Nombre jour -> '.$this->day_count;
    echo '<br>Annee -> '.$this->year;
    echo '<br>Numero mois -> '.$this->month_number;
    echo '<br>Jours -> ';
    var_dump($this->days);
  }

  public function display($apparts) {
    $table = "<table id='planning-table'> <tr> <td></td><th colspan='$this->day_count'>$this->month_name - $this->year</th> </tr>";
    $table .= "<tr><td></td>";
    foreach ($this->days as $day ) {
      $table .= "<td class='day'> $day</td>";
    }
    $table .= "</tr>";
    foreach ($apparts as $k=>$appart) {
      $table .= "<tr><td class='appart'>".str_replace('appartement-', "", $k)."</td>";
      foreach ($this->days as $day ) {
        $table .= "<td class='day free' id='$appart-$this->year-$this->month_number-$day'></td>";
      }
      $table .= "</tr>";
    }
    $table .= "</table>";

    return $table;
  }

  public static function getDaysCount($month, $year) {
    $time = mktime(0, 0, 0, $month, 1, $year);
    return date('t', $time);
  }

  /**
   * function getDatesBetween
   * renvoie un tableau contenant toutes les dates, jour par jour,
   * comprises entre les deux dates passées en paramètre.
   * NB : les dates doivent être au format aaaa-mm-dd (mais on peut changer le parsing)
   * @param (string) $dStart : date de départ
   * @param (string) $dEnd : date de fin
   * @return (array) aDates : tableau des dates si succès
   * @return (bool) false : si échec
   */
  public static function getInterval($dStart, $dEnd, $date_format="%Y-%m-%d") {
    $iStart = strtotime ($dStart);
    $aStart = explode ('-', $dStart);
    $iEnd = strtotime ($dEnd);
    if (false === $iStart || false === $iEnd) {
      return false;
    }
    $aEnd = explode ('-', $dEnd);
    if (count ($aStart) !== 3 || count ($aEnd) !== 3) {
      return false;
    }
    if (false === checkdate ($aStart[1], $aStart[2], $aStart[0]) || false === checkdate ($aEnd[1], $aEnd[2], $aEnd[0]) || $iEnd <= $iStart) {
      return false;
    }
    for ($i = $iStart; $i < $iEnd + 86400; $i = strtotime ('+1 day', $i) ) {
      $sDateToArr = strftime ($date_format, $i);
      $sYear = substr ($sDateToArr, 0, 4);
      $sMonth = substr ($sDateToArr, 5, 2);
      $aDates[] = $sDateToArr;
    }
    if (isset ($aDates) && !empty ($aDates)) {
      return $aDates;
    } else {
      return false;
    }
  }

  public static function test($datedeb, $datefin) {
    $interval = array();
    $dateref = explode("-", $datedeb);
    $deb = $dateref[2];
    $dateref = $dateref[0].'-'.$dateref[1].'-';
    $fin = explode("-", $datefin); $fin = $fin[2];
    for ($deb; $deb <= $fin; $deb++) {
      $displayDeb = (strlen($deb) == 1) ? "0".$deb : $deb;
      $interval[] = $dateref.$displayDeb;
    }
    return $interval;
  }
}

?>
